using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TriUan.TriUanGame.GameComponents.Polygon.Implements;
using TriUan.SCSEngine.Utils;
using TriUan.TriUanGameComponent.GameComponents;
using SCSEngine.Services;
using SCSEngine.ResourceManagement;
using Microsoft.Xna.Framework.Input.Touch;

namespace TriUan.TriUanGame.GameComponents
{
    public class PiecesManager
    {
        private SCSServices _service = null;
        private IResourceManager _resourceManager;

        private IRangeLimited _rangeLimited;
        List<Piece> _pieces;
        public List<Piece> Pices { get { return _pieces; } }

        public Piece LastPiece { get; set; }

        public PiecesManager(SCSServices service, IResourceManager resourceManager)
        {
            _service = service;
            _resourceManager = resourceManager;
            _rangeLimited = new RectangleRangeLimited();

            _pieces = new List<Piece>();
            _pieces.Add(loadPiece("1", "piecesXml\\1.xml"));
            _pieces.Add(loadPiece("2", "piecesXml\\2.xml"));
            _pieces.Add(loadPiece("3", "piecesXml\\3.xml"));
            _pieces.Add(loadPiece("4", "piecesXml\\4.xml"));
            _pieces.Add(loadPiece("5", "piecesXml\\5.xml"));
            _pieces.Add(loadPiece("6", "piecesXml\\6.xml"));
            _pieces.Add(loadPiece("7", "piecesXml\\7.xml"));
        }

        private Piece loadPiece(string fileTexture, string fileXml)
        {
            Piece pice;
            TUPolygon poligon;
            TUPieceInfo pieceInfo;
            Texture2D texture = _resourceManager.GetResource<Texture2D>(fileTexture);
            List<Vector2> listVerts = new List<Vector2>();
            Vector2 orig;


            XMLReader xml = new XMLReader(fileXml);
            XPoint xpoint = xml.ReadFile();
            orig = xml.GetCenter();

            foreach (Vector2 item in xpoint.mListOfVector)
            {
                listVerts.Add(item);
            }

            poligon = new TUPolygon(listVerts, orig);
            Random rand = new Random();
            pieceInfo = new TUPieceInfo(0f, 1f, new Vector2(77, 430f));//15f, 21f

            pice = new Piece(_service.GamePage, this, poligon, pieceInfo, texture);
            return pice;
        }

        public bool UpdateSpieces(GameTime gameTime)
        {
            TouchCollection touches = _service.InputHandle.TouchState;
            bool isTouch = false;
            if (LastPiece != null)
                LastPiece.Update(gameTime);
            else
            {
                foreach (var touch in touches)
                {
                    for (int i = _pieces.Count - 1; i >= 0; i--)
                    {
                        Piece piece = _pieces[i];
                        List<Vector2> listVert = piece.TransformVertices.Vertices;
                        if (_rangeLimited.CheckRange(listVert, (touch.Position - Camera2D.Pos)/Camera2D.Zoom) && !isTouch)
                        {
                            _pieces.Remove(piece);
                            _pieces.Add(piece);
                            piece.Update(gameTime);
                            LastPiece = piece;
                            isTouch = true;
                        }
                    }
                }
            }

            if (touches.Count == 0)
                LastPiece = null;

            if (LastPiece != null)
                return true;
            else
                return false;
        }

    }
}
